package graduationorder.graduationorder.Unit;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

public class OrderIdGenerator {

    private static final AtomicInteger sequence = new AtomicInteger(1); // 当天的序号
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

    public static synchronized String generateOrderId() {
        String datePart = dateFormat.format(new Date());
        int seq = sequence.getAndIncrement(); // 序号递增
        return datePart + String.format("%04d", seq); // 格式化序号
    }

    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                System.out.println(generateOrderId());
            }
        });
        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                System.out.println(generateOrderId());
            }
        });
        thread.start();
        thread1.start();
    }
}
